﻿# --------------------------------------------------------------- #
# Active Directory Domain Services (ADDS) Computer Configuration #
# --------------------------------------------------------------- #
param([string]$virtualMachineService, [string]$virtualMachineName)

Write-Information "Virtual machine '$virtualMachineName' executing computer '$($script.type)' configuration ... " -NoNewLine $true
$userName = $subscription.virtualMachines.adminUserName
$password = $subscription.virtualMachines.password
$netBiosDomain = $subscription.virtualMachines.netBiosDomain
$uri = Get-AzureWinRMUri -ServiceName $virtualMachineService -Name $virtualMachineName
$credential = New-Object System.Management.Automation.PSCredential(($netBiosDomain + "\" + $userName), (ConvertTo-SecureString $password -AsPlainText -Force))
$session = New-PSSession -ComputerName $uri[0].DnsSafeHost -Credential $credential -Port (Get-VirtualMachineInstanceEndpointPort $subscription $virtualMachineName) -UseSSL
switch ($script.type)
{
	"Remove"
	{
		Invoke-Command -Session $session -ScriptBlock {
			param([string]$serverName)
            Import-Module ActiveDirectory
		    if ((Get-ADComputer -Filter {Name -eq $serverName}) -ne $null)
            {
                Remove-ADComputer $serverName -Confirm:$false
            }
		} -ArgumentList $script.serverName
	}
	default
	{
		throw "Computer type '$($script.type)' is not supported."
	}
}
Remove-PSSession $session
Write-Text "done."